/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
**/

package com.chasekernan.hxnova.utils;

#if flash9

import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;

class Translator implements Dynamic<String> {
    
    //english examples
    /**
    public static var BORANIUM              = "Boranium";
	public static var DRAW_GRID             = "Draw Grid";
	public static var FLEETS                = "Fleets";
	public static var GERMANIUM             = "Germanium";
	public static var GRAVITY               = "Gravity";
	public static var IRONIUM               = "Ironium";
	public static var MAP                   = "Map";
	public static var OWNER                 = "Owner";
	public static var POPULATION            = "Population";
	public static var RADIATION             = "Radiation";
	public static var SALVAGE               = "Salvage";
	public static var SHOW_FLEET_AMOUNTS    = "Show Fleet Amounts";	
    public static var SHOW_FLEET_NAMES      = "Show Fleet Names";
	public static var SHOW_FLEETS           = "Show Fleets";
	public static var SHOW_SCANNERS         = "Show Scanners";
	public static var SHOW_STAR_NAMES       = "Show Star Names";
	public static var SHOW_STAR_OWNER       = "Show Star Owner";
	public static var STARS                 = "Stars";
	public static var TEMPERATURE           = "Temperature";
	public static var UNINHABITED           = "Uninhabited";
	public static var UNKNOWN               = "Unknown";
	public static var UNKNOWN_FLEET         = "Unknown Fleet";
	public static var ZOOM                  = "Zoom";
    **/
    
    private static var translations = new Hash<String>();
		
	public static function init(onCompletion : Void -> Void, ?language = "en") {
        var path = "data/" + language + ".xml";
		var loader = new URLLoader(new URLRequest(path));
        
		loader.addEventListener(Event.COMPLETE, 
            function(_) {
                var xml = Xml.parse(loader.data).firstElement();
                
                for (element in xml.elements()) {
                    translations.set(element.nodeName, 
                                     element.firstChild().nodeValue);
                }
                
                onCompletion();
            }
        );
	}
    
    public static function resolve(key : String) : String {
        return translations.get(key);
    }
    
    public static function set(key : String, to : String) {
        translations.set(key, to);
    }
}
#end